<?php

class Ecardeapi
{
    // protected $CONFIG_OBJ;
    protected $DBTABLE;

    //URL value passed into constructor
    protected $URL;
    protected $TABLE;
    protected $ID;
    protected $FIELD;
    protected $DELETED;
    protected $CONFIG;
    //SQL ELEMENTS
    protected $SELECT = '*';
    protected $TABLES = '';
    protected $WHERE = '';
    protected $ORDERBY = '';
    protected $GROUPBY = '';
    protected $LIMIT = '';
    //SET OF DATA LOADED
    protected $DATA;

    public function __construct($_config)
    {
        global $SMARTY,$DBobject,$Cardclient;
        $this->URL = $_config->url;
        $this->ID = $_config->api->id;
        $this->FIELD = $_config->api->field;
        $this->CONFIG = $_config;
    }

    public function getRecord($id)
    {
        global $SMARTY,$DBobject;
        $article_f = array();
        if ($this->CONFIG->table->where->attributes()->notedit) {
            $this->WHERE = null;
        }
        $sql = "SELECT * FROM {$this->TABLE} WHERE {$this->ID} = '{$id}' AND {$this->DELETED} IS NULL".($this->WHERE != '' ? " AND {$this->WHERE}" : '');// AND article_deleted IS NULL";
        //echo $sql.'<br/>';
        if ($res = $DBobject->wrappedSqlGet($sql)) {
            foreach ($res[0] as $key => $field) {
                if (!is_numeric($key)) {
                    $article_f[$key] = $field;
                }
            }
            foreach ($this->CONFIG->table->associated as $a) {
                $article_f["{$a->name}"] = $this->getAssociated($a, $res[0]["{$a->linkfield}"]);
            }
            foreach ($this->CONFIG->table->log as $a) {
                $article_f['logs'] = $this->getLog($a, $res[0]["{$a->id}"]);
            }
            foreach ($this->CONFIG->table->extends as $a) {
                $pre = str_replace('tbl_', '', $a->table);
                $sql = "SELECT * FROM {$a->table} WHERE {$a->field} = '".$res[0]["{$a->linkfield}"]."' AND {$pre}_deleted IS NULL ";
                if ($res2 = $DBobject->wrappedSqlGet($sql)) {
                    foreach ($res2[0] as $key => $field) {
                        if (empty($listing_f["{$key}"])) {
                            $article_f["{$key}"] = $field;
                        } else {
                            if (!is_array($listing_f["{$key}"])) {
                                $temp = $listing_f["{$key}"];
                                $article_f["{$key}"] = array(
                                  $temp,
                                );
                            }
                            $article_f["{$key}"][] = $field;
                        }
                    }
                }
            }
        }
        foreach ($this->CONFIG->table->options->field as $f) {
            if ($f->attributes()->recursive) {
                $parentID = 0;
                if ($this->CONFIG->root_parent_id) {
                    $parentID = $this->CONFIG->root_parent_id;
                }
                $article_f ['options'] ["{$f->name}"] = $this->getOptionsCatTree($f, $parentID);
            } else {
                $pre = str_replace('tbl_', '', $f->table);

                $extends = '';
                foreach ($f->extends as $e) {
                    $extends .= " LEFT JOIN {$e->table} ON {$e->linkfield} = {$e->field}";
                }

                $ref = array();
                $ext = array();
                foreach ($f->reference as $r) {
                    $ref[] = $r;
                }
                foreach ($f->extra as $r) {
                    $ext[] = $r;
                }
                $sql = "SELECT {$f->id},".implode(',', $ref).(empty($ext) ? '' : ','.implode(',', $ext))." FROM {$f->table} {$extends} WHERE {$pre}_deleted IS NULL ".($f->where != '' ? "AND {$f->where} " : '').' '.($f->orderby != '' ? " ORDER BY {$f->orderby} " : '');
                if ($res = $DBobject->wrappedSqlGet($sql)) {
                    foreach ($res as $row) {
                        $value = array();
                        foreach ($ref as $r) {
                            $value[] = $row ["{$r}"];
                        }
                        $extra = array();
                        foreach ($ext as $r) {
                            $extra["{$r}"] = $row ["{$r}"];
                        }
                        $article_f ['options'] ["{$f->name}"] [] = array(
                                'id' => $row ["{$f->id}"],
                                'value' => implode(' ', $value),
                                'extra' => $extra,
                        );
                    }
                }
            }
        }

        return  $article_f;
    }

    public function getApiList($hierarchy_id = null, $level = 0)
    {
        global $SMARTY,$DBobject,$Cardclient;

        $records = $data = [];
        foreach ($this->CONFIG->api->data->children() as $child) {
            $data[$child->getName()] = (string) $child;
        }
        //var_dump($data);exit;
        $action = $this->CONFIG->api->action;
        $response_data = $Cardclient->CallHttpPost($action, $data);
        if ($response_data ['status'] === 0) {
            $res = $response_data ['data'];
        }

        if (is_array($res)) {
            foreach ($res as $key => $val) {
                $n = 0;
                $title = '';
                foreach ($this->FIELD as $f) {
                    $title .= ($n > 0 ? ', ' : '').$val["{$f}"];
                    ++$n;
                }
                $records[$val["{$this->ID}"]] = array(
                        'title' => $title,
                        'record' => $val,
                        'id' => $val["{$this->ID}"],
                        'url' => "/admin/edit/{$this->URL}/{$val["{$this->ID}"]}",
                        'url_delete' => "/admin/delete/{$this->URL}/{$val["{$this->ID}"]}",
                        'published' => 1,
                );
            }
            //var_dump($records);exit;
        }

        return  $records;
    }
    public function deleteRecord($id)
    {
        global $DBobject;
        try {
            $sql = "UPDATE {$this->TABLE} SET {$this->DELETED} = NOW()".(!empty($this->PUBLISHED) ? ",{$this->PUBLISHED} = 0" : '')." WHERE {$this->ID} = '{$id}' ";
            if ($DBobject->executeSQL($sql)) {
                global $DELETED;
                $_SESSION['notice'] = $DELETED;
                saveInLog('Delete', $this->TABLE, $id);

                return true;
            }
        } catch (Exception $e) {
            $sql = "UPDATE {$this->TABLE} SET {$this->DELETED} = NOW() WHERE {$this->ID} = '{$id}' ";
            if ($DBobject->executeSQL($sql)) {
                global $DELETED;
                $_SESSION['notice'] = $DELETED;
                saveInLog('Delete', $this->TABLE, $id);

                return true;
            }
        }

        return false;
    }

    public function getLog($a, $id)
    {
        global $SMARTY,$DBobject;
        $results = array();

        if ((string) $a->id != (string) $a->field) {
            $sql = "SELECT {$a->field} FROM {$a->table} WHERE {$a->id} = :id";
            $parent = $DBobject->wrappedSqlGet($sql, array(':id' => $id));

            $sql = "SELECT tbl_log.*, {$a->id}, admin_name FROM tbl_log LEFT JOIN tbl_admin ON admin_id = log_admin_id LEFT JOIN {$a->table} ON {$a->id} = log_record_id WHERE log_record_table = :table AND log_deleted IS NULL AND {$a->field} = :fid ORDER BY log_created DESC";
            $results = $DBobject->wrappedSqlGet($sql, array(':table' => $a->table, ':fid' => $parent[0]["{$a->field}"]));
        } else {
            $sql = 'SELECT tbl_log.*, tbl_admin.admin_name FROM tbl_log LEFT JOIN tbl_admin ON admin_id = log_admin_id WHERE log_record_table = :table AND log_record_id = :id AND log_deleted IS NULL ORDER BY log_created DESC';
            $results = $DBobject->wrappedSqlGet($sql, array(':table' => $a->table, ':id' => $id));
        }

        return $results;
    }
}
